package com.sunlg.smarthome.contract.presenter;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.LoadRainObserverContract;
import com.sunlg.smarthome.data.entity.RainObserver;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;
import com.sunlg.smarthome.utils.PreferHelper;

import java.util.List;

/**
 * Created by sunlg on 2017/6/2.
 */

public class LoadRainObserverPresenter implements LoadRainObserverContract.Presenter {

    private SHServiceWrapper shServiceWrapper;
    private PreferHelper preferHelper;
    private LoadRainObserverContract.View mView;

    public LoadRainObserverPresenter() {
        shServiceWrapper = App.getInstance().getServiceWrapper();
        preferHelper = new PreferHelper(App.getInstance(), "gateway");
    }

    @Override
    public void loadRainObserver() {
        shServiceWrapper.getAllRainObserver(preferHelper.getInt("id", 0),
                new MySubscriber<Result<List<RainObserver>>>() {
                    @Override
                    public void onSuccess(Result<List<RainObserver>> data) {
                       if(data.getCode() == C.ActionStatus.SUCCESS){
                           mView.loadRainObserverSuccess(data.getData().get(0));
                       }else {
                           _onError(new Throwable(data.getMsg()));
                       }
                    }

                    @Override
                    public void _onError(Throwable e) {
                        mView.showNoDevice();
                        mView.showError(e.getMessage());
                    }
                });
    }

    @Override
    public void attachView(LoadRainObserverContract.View view) {
        mView = view;
    }

    @Override
    public void dettachView() {
        mView = null;
    }
}
